package com.leiqichao.backend.controller;


import com.leiqichao.backend.common.Page;
import com.leiqichao.backend.common.Result;
import com.leiqichao.backend.entity.LeaveMessage;
import com.leiqichao.backend.interceptor.StatsInterceptor;
import com.leiqichao.backend.service.LeaveMessageService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api/messages")
public class LeaveMessageController {

    @Autowired
    private  LeaveMessageService leaveMessageService;


    @Resource
    StatsInterceptor statsInterceptor;


    @RequestMapping("/page")
    public Result<Page> getAllLeaveMessages(@RequestBody Page<LeaveMessage> page) {
        return Result.success(leaveMessageService.getAllLeaveMessages(page));
    }

    @PostMapping("/save")
    public Result<Map<String, Object>> saveleaveMessage(HttpServletRequest request,
                                                       @RequestBody(required = false) LeaveMessage leaveMessage) {
        String clientIp = statsInterceptor.getClientIp(request);
        leaveMessage.setIp(clientIp);
        return Result.success(leaveMessageService.saveleaveMessage(leaveMessage));
    }

    @RequestMapping("/update")
    public Result<Boolean> update(LeaveMessage leaveMessage) {
        return Result.success(leaveMessageService.update(leaveMessage));
    }

    @RequestMapping("/remove")
    public  Result<Boolean> remove(String id) {

        return Result.success(leaveMessageService.remove(id));
    }

    @RequestMapping("/get")
    public Result<LeaveMessage> get(String id) {
        return Result.success(leaveMessageService.get(id));
    }
}
